import piniaStore from '@/store'

// 验证是否为空
export const hasContent = (content:string) => {
  return content.replace(/ +/g, '').replace(/[\r\n]/g, '') !== ''
}
// 用户名验证
export function validUsername (str:string) {
  return hasContent(str)
}

// 密码验证
// 密码必须包含大小写字母、数字和特殊字符（长度大于6位）
export function validPassword (password:string) {
  const abpConfig = piniaStore.permissionStore.abpConfig
  const setting = abpConfig.setting.values
  const RequireDigit = setting['Abp.Identity.Password.RequireDigit'] === 'True'
  const RequireLowercase = setting['Abp.Identity.Password.RequireLowercase'] === 'True'
  const RequireNonAlphanumeric = setting['Abp.Identity.Password.RequireNonAlphanumeric'] === 'True'
  const RequireUppercase = setting['Abp.Identity.Password.RequireUppercase'] === 'True'
  const RequiredLength = Number(setting['Abp.Identity.Password.RequiredLength'])
  const RequiredUniqueChars = Number(setting['Abp.Identity.Password.RequiredUniqueChars'])
  const result: any[] = []
  if (RequiredLength > 0 && !requiredLength(password, RequiredLength)) {
    result.push('长度不能小于' + RequiredLength + '位')
  }
  if (RequireDigit && !requiredDigit(password)) {
    result.push('密码必须包含数字')
  }
  if (RequireLowercase && !requiredLowercase(password)) {
    result.push('密码必须包含小写字母')
  }
  if (RequireNonAlphanumeric && !requiredNonAlphanumeric(password)) {
    result.push('密码必须包含非字母数字字符')
  }
  if (RequireUppercase && !requiredUppercase(password)) {
    result.push('密码必须包含大写字母')
  }
  if (RequiredUniqueChars && !requiredUniqueChars(password, RequiredUniqueChars)) {
    result.push('密码必须包含' + RequiredUniqueChars + '个不同字符')
  }
  return result
}

export function requiredDigit (value:string) {
// 正则: 必须包含数字
  const reg = /^(?=.*\d)/
  return reg.test(value)
}
export function requiredLowercase (value: string) {
  // 正则: 必须包含小写 ASCII 字符
  const reg = /^(?=.*[a-z])/
  return reg.test(value)
}

export function requiredNonAlphanumeric (value:string) {
  // 正则: 必须包含非字母数字字符
  const reg = /^(?=.*[^a-zA-Z0-9])/
  return reg.test(value)
}

export function requiredUppercase (value:string) {
  // 正则：必须包含大写 ASCII 字符
  const reg = /^(?=.*[A-Z])/
  return reg.test(value)
}

export function requiredLength (value:string, count: number):boolean {
  // 必须达到的最小长度
  const length = value.length
  return length >= count
}

export function requiredUniqueChars (value:string, count:number):boolean {
  const wordsObj:Record<string, number> = {}
  for (let index = 0; index < value.length; index++) {
    const word = value[index]
    word in wordsObj ? wordsObj[word]++ : wordsObj[word] = 1
  }
  const maxNum = Object.keys(wordsObj).length
  return maxNum >= count
}

export const validateUsername = (rule: any, value: any, callback: any) => {
  if (!validUsername(value)) {
    callback(new Error('请输入登录名'))
  } else {
    callback()
  }
}
export const validatePassword = (rule: any, value: any, callback: any) => {
  const result = validPassword(value)
  if (result.length > 0) {
    callback(
      new Error(result[0])
    )
  }
  callback()
}
